<?php

class ContactController extends Australian_Controller_Action {

    // Mang tham so nhan duoc o moi Action
    protected $_arrParam;
    // Duong dan cua Controller
    protected $_currentController;
    // Duong dan cua Action chinh
    protected $_actionMain;

    public function init() {
        // Mang tham so nhan duoc o moi Action
        $this->_arrParam = $this->_request->getParams();

        $this->_arrParam['module'] = (isset($this->_arrParam['module'])) ? $this->_arrParam['module'] : "default";
        // Duong dan cua Controller
        $this->_currentController = '/' . $this->_arrParam['module']
                . '/' . $this->_arrParam['controller'];

        // Duong dan cua Action chinh
        $this->_actionMain = '/' . $this->_arrParam['module']
                . '/' . $this->_arrParam['controller'] . '/index';

        // Truyen ra view
        $this->view->arrParam = $this->_arrParam;
        $this->view->currentController = $this->_currentController;
        $this->view->actionMain = $this->_actionMain;

        parent::init();
        $template_path = TEMPLATE_PATH . "/public/system";
        $this->loadTemplate($template_path, 'template.ini', 'template');
    }

    public function indexAction() {

        $tblNews = new Default_Model_News();
        $this->view->tblNewsFooter = $tblNews->newsList($this->_arrParam, array('task' => 2));
    }

    public function sendmailAction() {
        if (isset($_POST['action']) && $_POST['action'] != '' && $_POST['action'] == 'sendemail') {

            // Edit these two lines with your email address and the subject that you want to receive
            $email_to               = Australian_Utilities::email_admin;
            $email_subject          = "You've got an email from your website!";

            // - Do not edit below this line :)
            if (!isset($_POST['name']) ||
                    !isset($_POST['email']) ||
                    !isset($_POST['message'])) {
                die('');
            }
            $arrayInfo = array();
            $name = $_POST['name'];
            if (preg_match("/'/", $name)) {
                $name = str_replace("'", "\'", $name);
            }
            $email = $_POST['email'];
            $message = $_POST['message'];
            $email_message = "";

//            $email_message = "Hello Admin! You have a new email .\n\n";

            function clean_string($string) {
                $bad = array("content-type", "bcc:", "to:", "cc:", "href");
                return str_replace($bad, "", $string);
            }

//            $email_message .= "Name: " . clean_string($name) . "\n";
//            $email_message .= "Email: " . clean_string($email) . "\n";
            $email_message .= clean_string($message);

            $headers = 'From: ' . $email . "\r\n" .
                    'Reply-To: ' . $email . "\r\n" .
                    'X-Mailer: PHP/' . phpversion();


            $arrayInfo['name'] = trim($name);
            $arrayInfo['email'] = trim($email);
            $arrayInfo['message'] = trim($email_message);

            $tbContact = new Default_Model_Contact();
            $tbContact->insert($arrayInfo, "contact");
            mail($email_to, $email_subject, $email_message, $headers);
            die();
        }

        if (isset($_POST['action']) && $_POST['action'] != '' && $_POST['action'] == 'newslettersignup') {

            
            // - Do not edit below this line :)
            if (!isset($_POST['email'])) {
                die('');
            }
            // Edit these two lines with your email address and the subject that you want to receive
            $email_to = trim($_POST['email']);
            $email_subject = "News Lettersignup from DU HOC AUSTRALIA website!";

            $arrayInfo = array();
            $email_message = "";
            $email_message .= "You have registered to receive information from website .\n";
            $email_message .= " Thank you !\n\n";
            $email_message .= " This is the email sent automatically, please do not reply !";
            $arrayInfo['email'] = trim($email_to);
            $headers = 'From: ' . " Admin AUSTRALIAN Website \r\n" .
                    'Reply-To: ' . " No reply\r\n" .
                    'X-Mailer: PHP/' . phpversion();
            $tbContact = new Default_Model_Contact();
            $tbContact->insert($arrayInfo, "newslettersignup");
            mail($email_to, $email_subject, $email_message, $headers);
            die();
        }
    }

}